﻿
public class NetHandler_FetchMoments : GameServerMessageHandler
{
    public const string Key = "FetchMoments";

    public static NetHandler_FetchMoments NewHandler()
    {
        return new NetHandler_FetchMoments();
    }

    protected override ISerialize NewData() => new FetchUserMomentsRequestData();

    public override void _Execute(TCPClient client, ISerialize _data)
    {
        FetchUserMomentsRequestData requestData = _data as FetchUserMomentsRequestData;

        GameDebug.Log($"用户{client.userID} 获取动态:{requestData?.communityType}");

        UserServer user = Game.instance.GetUser(client.userID);

        FetchMomentsResponseData response = new FetchMomentsResponseData();

        if (user == null)
        {
            GameDebug.LogError($"找不到用户：{client.userID}");
            response.code = ErrorCode.UserNotFound;
        }
        else
        {
            if (requestData != null)
            {
                List<moments> momentTables = new List<moments>();

                int communityType = requestData.communityType;
                int beginIndex = requestData.beginIndex;
                int gender = requestData.gender;

                if(communityType == CommunityType.Follow)
                {
                    List<long> myFollows = user.data.myFollows;
                    foreach(var myFollow in myFollows)
                    {
                        momentTables.AddRange(UserServer.FindInMomentsTableWithUserID(myFollow));
                    }

                }
                else if (communityType == CommunityType.SameCity)
                {
                    momentTables.AddRange(UserServer.SelectMomentsTableDesc(user.data.userInfo.locationCity,requestData.num + beginIndex));
                }
                else if(communityType == CommunityType.Recommand)
                {
                    momentTables.AddRange(UserServer.SelectMomentsTableDesc(requestData.num + beginIndex));
                }

                momentTables.Sort((a, b) => b.time.CompareTo(a.time));

                if (momentTables.Count > requestData.num)
                {
                    if (beginIndex > 0)
                    {
                        if (beginIndex < momentTables.Count)
                        {
                            int num = beginIndex + requestData.num > momentTables.Count ? momentTables.Count - beginIndex : requestData.num;
                            momentTables = momentTables.GetRange(beginIndex, num);
                        }
                        else
                        {

                            momentTables = momentTables.GetRange(momentTables.Count - requestData.num, requestData.num);
                        }

                    }
                    else
                        momentTables = momentTables.GetRange(0, requestData.num);
                }

                List<UserMoment> moments = new List<UserMoment>();

                for (int i = 0; i < momentTables.Count; i++)
                {
                    moments table = momentTables[i];

                    if (table == null) 
                        continue;

                    long deviceId = table.uid;
                    UserInfo newUserInfo = UserServer.GetUserInfoWithUserId(deviceId);

                    if (newUserInfo == null)
                        continue;

                    Moment moment = Table2Moment(table);

                    UserMoment userMoment = new UserMoment();

                    userMoment.miniUserInfo = new MiniUserInfo(newUserInfo);
                    userMoment.miniUserInfo.gender = newUserInfo.gender;
                    userMoment.miniUserInfo.name = newUserInfo.name;
                    userMoment.miniUserInfo.birthYear = newUserInfo.birthYear;
                    userMoment.miniUserInfo.locationProvince = newUserInfo.locationProvince;
                    userMoment.miniUserInfo.locationCity = newUserInfo.locationCity;
                    userMoment.miniUserInfo.job = newUserInfo.job;
                    userMoment.miniUserInfo.eduation = newUserInfo.eduation;
                    userMoment.miniUserInfo.height = newUserInfo.height;
                    userMoment.miniUserInfo.icon = newUserInfo.icon;

                    //Tex tex1 = TextureSaver.LoadMomentPhoto(deviceId, table.id, 1);
                    //Tex tex2 = TextureSaver.LoadMomentPhoto(deviceId, table.id ,2);
                    //Tex tex3 = TextureSaver.LoadMomentPhoto(deviceId, table.id ,3);
                    //Tex tex4 = TextureSaver.LoadMomentPhoto(deviceId, table.id ,4);
                    //Tex tex5 = TextureSaver.LoadMomentPhoto(deviceId, table.id ,5);
                    //Tex tex6 = TextureSaver.LoadMomentPhoto(deviceId, table.id ,6);
                    //Tex tex7 = TextureSaver.LoadMomentPhoto(deviceId, table.id ,7);
                    //Tex tex8 = TextureSaver.LoadMomentPhoto(deviceId, table.id ,8);
                    //Tex tex9 = TextureSaver.LoadMomentPhoto(deviceId, table.id, 9);
                    //Tex[] texes = new[] { tex1, tex2, tex3, tex4, tex5, tex6, tex7, tex8, tex9 };

                    //List<long> commentIds = users.String2LongList(table.comments);

                    //List<Comment> commentsArr = new List<Comment>(commentIds.Count);
                    //for(int j = 0; j < commentIds.Count; j++)
                    //{
                    //    var commentTable = UserServer.FindInCommentTable(deviceId, commentIds[j]);
                    //    if (commentTable == null)
                    //        continue;

                    //    List<SubComment> subComments = null;

                    //    if (!string.IsNullOrEmpty(commentTable.subcomments))
                    //    {
                    //        subComments = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SubComment>>(commentTable.subcomments);
                    //    }

                    //    UserInfo commentUserInfo = UserServer.GetUserInfoWithUserId(commentTable.uid);
                    //    if (commentUserInfo == null)
                    //    {
                    //        GameDebug.LogError($"cant find user with:{commentTable.uid}");
                    //        continue;
                    //    }
                    //    MiniUserInfo miniUserInfo = new MiniUserInfo(commentUserInfo);

                    //    var comment = new Comment(deviceId, commentTable.moment_id, commentTable.id, miniUserInfo, commentTable.content,
                    //    users.String2LongList(commentTable.likes), commentTable.time, subComments);

                    //    commentsArr.Add(comment);
                    //}

                    //userMoment.moment = new Moment(deviceId, table.id, table.time, table.content, table.location, table.topic
                    //    , users.String2LongList(table.likes), commentsArr, texes) ;

                    userMoment.moment = moment;
                    moments.Add(userMoment);

                    GameDebug.Log($"get moment:{userMoment}");
                }
                response.moments = moments;

                response.code = 0;
            }
            else
            {
                GameDebug.LogError($"requestData == null _data:{_data != null} type:{_data.GetType()}");
                response.code = -1;
            }
        }

        byte[] data = response.Write();
        this.SendResponse(client, Key, this.m_MessageReceived.seq, data);
    }

    public static Moment Table2Moment(moments table)
    {
        if (table == null)
            return null;

        long deviceId = table.uid;

        //Tex tex1 = TextureSaver.LoadMomentPhoto(deviceId, table.id, 1);
        //Tex tex2 = TextureSaver.LoadMomentPhoto(deviceId, table.id, 2);
        //Tex tex3 = TextureSaver.LoadMomentPhoto(deviceId, table.id, 3);
        //Tex tex4 = TextureSaver.LoadMomentPhoto(deviceId, table.id, 4);
        //Tex tex5 = TextureSaver.LoadMomentPhoto(deviceId, table.id, 5);
        //Tex tex6 = TextureSaver.LoadMomentPhoto(deviceId, table.id, 6);
        //Tex tex7 = TextureSaver.LoadMomentPhoto(deviceId, table.id, 7);
        //Tex tex8 = TextureSaver.LoadMomentPhoto(deviceId, table.id, 8);
        //Tex tex9 = TextureSaver.LoadMomentPhoto(deviceId, table.id, 9);
        var tex1 = TextureSaver.GetMomentPhotoUrl(deviceId, table.id, 1);
        var tex2 = TextureSaver.GetMomentPhotoUrl(deviceId, table.id, 2);
        var tex3 = TextureSaver.GetMomentPhotoUrl(deviceId, table.id, 3);
        var tex4 = TextureSaver.GetMomentPhotoUrl(deviceId, table.id, 4);
        var tex5 = TextureSaver.GetMomentPhotoUrl(deviceId, table.id, 5);
        var tex6 = TextureSaver.GetMomentPhotoUrl(deviceId, table.id, 6);
        var tex7 = TextureSaver.GetMomentPhotoUrl(deviceId, table.id, 7);
        var tex8 = TextureSaver.GetMomentPhotoUrl(deviceId, table.id, 8);
        var tex9 = TextureSaver.GetMomentPhotoUrl(deviceId, table.id, 9);
        var texes = new[] { tex1, tex2, tex3, tex4, tex5, tex6, tex7, tex8, tex9 };

        List<long> commentIds = users.String2LongList(table.comments);

        List<Comment> commentsArr = new List<Comment>(commentIds.Count);
        for (int j = 0; j < commentIds.Count; j++)
        {
            var commentTable = UserServer.FindInCommentTable(deviceId, commentIds[j]);
            if (commentTable == null)
                continue;

            List<SubComment> subComments = null;

            if (!string.IsNullOrEmpty(commentTable.subcomments))
            {
                subComments = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SubComment>>(commentTable.subcomments);
            }

            UserInfo commentUserInfo = UserServer.GetUserInfoWithUserId(commentTable.uid);
            if (commentUserInfo == null)
            {
                GameDebug.LogError($"cant find user with:{commentTable.uid}");
                continue;
            }
            MiniUserInfo miniUserInfo = new MiniUserInfo(commentUserInfo);

            var comment = new Comment(deviceId, commentTable.moment_id, commentTable.id, miniUserInfo, commentTable.content,
            users.String2LongList(commentTable.likes), commentTable.time, subComments);

            commentsArr.Add(comment);
        }

        var moment = new Moment(deviceId, table.id, table.time, table.content, table.location, table.topic
            , users.String2LongList(table.likes), commentsArr, texes);

        return moment;

    }
}
